package com.quhaodian.user.data.service.impl;

import com.quhaodian.data.core.Finder;
import com.quhaodian.user.data.dao.UserInfoDao;
import com.quhaodian.user.data.entity.UserInfo;
import com.quhaodian.user.oauth.domain.OauthResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.quhaodian.data.core.Updater;
import com.quhaodian.user.data.dao.UserOauthTokenDao;
import com.quhaodian.user.data.entity.UserOauthToken;
import com.quhaodian.user.data.service.UserOauthTokenService;

import com.quhaodian.data.page.Filter;
import com.quhaodian.data.page.Order;
import com.quhaodian.data.page.Page;
import com.quhaodian.data.page.Pageable;

import java.util.List;

import com.quhaodian.data.utils.FilterUtils;


/**
 * Created by imake on 2017年07月21日16:04:29.
 */
@Service
@Transactional
public class UserOauthTokenServiceImpl implements UserOauthTokenService {

    private UserOauthTokenDao dao;

    @Autowired
    private UserInfoDao userInfoDao;


    @Override
    public UserOauthToken findByUid(String uid) {


        Finder finder = Finder.create();
        finder.append("from UserOauthToken u where u.uid=:uid");
        finder.setParam("uid", uid);
        return dao.findOne(finder);
    }

    @Override
    public UserOauthToken login(OauthResponse response) {

        Finder finder = Finder.create();
        finder.append("from UserOauthToken u where u.uid=:uid");
        finder.append(" and u.token_type=:token_type");
        finder.setParam("uid", response.getOpenid());
        finder.setParam("token_type", response.type());
        UserOauthToken token = dao.findOne(finder);
        if (token==null){
            token=new UserOauthToken();
            token.setUid(response.getOpenid());
            token.setToken_type(response.type());
            UserInfo user=new UserInfo();
            user.setAvatar(response.getAvatar());
            user.setName(response.getName());
            userInfoDao.save(user);
            token.setUser(user);
            dao.save(token);
        }
        return token;
    }

    @Override
    @Transactional(readOnly = true)
    public UserOauthToken findById(Long id) {
        return dao.findById(id);
    }


    @Override
    @Transactional
    public UserOauthToken save(UserOauthToken bean) {
        dao.save(bean);
        return bean;
    }

    @Override
    @Transactional
    public UserOauthToken update(UserOauthToken bean) {
        Updater<UserOauthToken> updater = new Updater<UserOauthToken>(bean);
        return dao.updateByUpdater(updater);
    }

    @Override
    @Transactional
    public UserOauthToken deleteById(Long id) {
        UserOauthToken bean = dao.findById(id);
        dao.deleteById(id);
        return bean;
    }

    @Override
    @Transactional
    public UserOauthToken[] deleteByIds(Long[] ids) {
        UserOauthToken[] beans = new UserOauthToken[ids.length];
        for (int i = 0, len = ids.length; i < len; i++) {
            beans[i] = deleteById(ids[i]);
        }
        return beans;
    }


    @Autowired
    public void setDao(UserOauthTokenDao dao) {
        this.dao = dao;
    }

    @Override
    public Page<UserOauthToken> page(Pageable pageable) {
        return dao.page(pageable);
    }


    @Override
    public Page<UserOauthToken> page(Pageable pageable, Object search) {
        List<Filter> filters = FilterUtils.getFilters(search);
        if (filters != null) {
            pageable.getFilters().addAll(filters);
        }
        return dao.page(pageable);
    }

    @Override
    public List<UserOauthToken> list(int first, Integer size, List<Filter> filters, List<Order> orders) {
        return dao.list(first, size, filters, orders);
    }
}